#include "drv_led.h"
#include "drv_beep.h"
#include "drv_key.h"
#include "drv_usart.h"
#include "drv_exti.h"
#include "string.h"
#include "drv_time.h"
#include "drv_iic.h"
#include "oled.h"
#include "bmp.h"

uint8_t key_flag1 = 0;
uint8_t key_flag2 = 1;
uint8_t key_flag3 = 2;
uint8_t key_flag4 = 3;

uint8_t dir = 0;
uint32_t led_pwm = 0;

u8 t=' ';
	uint8_t flag = 0;

int main()
{
//	drv_beep_init();
	drv_led_init();//led灯初始化
	drv_systick_init(168);//systick定时器初始化
	drv_key_init(GPIOE,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);//按键初始化
	drv_key_init2(GPIOC,GPIO_Pin_13);
//	drv_usart_init();//串口初始化
	drv_exti_init();
//	drv_time6_init();
//	drv_time1_init();
	drv_iic_init();
	OLED_Init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择中断组

	
	//OLED屏幕
	OLED_ShowPicture(0,0,128,64,BMP1,1);
	OLED_Refresh();
	drv_systick_delayms(500);
	OLED_Clear();

	OLED_ShowChinese(17,8,0,16,1);
	OLED_ShowChinese(43,8,1,16,1);
	OLED_ShowChinese(69,8,2,16,1);
	OLED_ShowChinese(95,8,3,16,1);
	OLED_ShowString(8,24,"HUAQINGYUANJIAN",16,1);
	OLED_ShowString(20,40,"2024/03/06",16,1);
	
	OLED_Refresh();
	
//	printf("hello 2311 class \r\n");

	while(1)
	{	
		if(flag != 0)
		{
			OLED_Clear();
		}
		flag = 0;
		if(key_flag1 == 0)
		{
			drv_led_close(GPIO_Pin_8);
			OLED_ShowString(0, 0, "LED1 : close", 16, 1);
		}
		else
		{
			drv_led_open(GPIO_Pin_8);
			OLED_ShowString(0, 0, "LED1 : open", 16, 1);
		}
		if(key_flag2 == 1)
		{
			drv_led_close(GPIO_Pin_9);
			OLED_ShowString(0, 16, "LED2 : close", 16, 1);
		}
		else
		{
			drv_led_open(GPIO_Pin_9);
			OLED_ShowString(0, 16, "LED2 : open", 16, 1);
		}
		if(key_flag3 == 2)
		{
			drv_beep_close();
			OLED_ShowString(0, 32, "BEEP : close", 16, 1);
		}
		else
		{
			drv_beep_open();
			OLED_ShowString(0, 32, "BEEP : open", 16, 1);
		}

		if(flag != 0)
		{
			OLED_Refresh();
		}
		if(get_key_value(GPIOE,GPIO_Pin_4) == 1)
		{
			key_flag1 = ~key_flag1;
			flag = 1;
		}
		if(get_key_value(GPIOE,GPIO_Pin_5) == 1)
		{
			key_flag2 = ~key_flag2;
			flag = 1;
		}
		if(get_key_value(GPIOE,GPIO_Pin_6) == 1)
		{
			key_flag3 = ~key_flag3;
			flag = 1;
		}
		drv_systick_delayms(10);
	}
}





#if 0
int main(void)
{
	drv_led_init();//led灯初始化
	drv_systick_init(168);//systick定时器初始化
	drv_key_init(GPIOE,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6);//按键初始化
	drv_key_init2(GPIOC,GPIO_Pin_13);
	drv_usart_init();//串口初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择中断组
	printf("hello 2311 class \r\n");

	while(1)
	{
		if(key_flag4 == 3 )
		{
			if(key_flag1 == 0 )
			{
					drv_led_close(GPIO_Pin_8);
			}
			else
			{
					drv_led_open(GPIO_Pin_8);
			}
			if(key_flag2 == 1 )
			{
					drv_led_close(GPIO_Pin_9);
			}
			else
			{
					drv_led_open(GPIO_Pin_9);
			}
			if(key_flag3 == 2 )
			{
					drv_led_close(GPIO_Pin_10);
			}
			else
			{
					drv_led_open(GPIO_Pin_10);
			}
		}
		else
		{
			drv_led_open(GPIO_Pin_8);
			drv_systick_delayms(500);
			
			drv_led_open(GPIO_Pin_9);
			drv_systick_delayms(500);

			drv_led_open(GPIO_Pin_10);
			drv_systick_delayms(500);
			
			drv_led_close(GPIO_Pin_10);
			drv_led_close(GPIO_Pin_9);
			drv_led_close(GPIO_Pin_8);
			drv_systick_delayms(500);
		}
		
		//获取键值
		if(get_key_value(GPIOE,GPIO_Pin_4))
		{
			key_flag1 = ~key_flag1;
		}
		if(get_key_value(GPIOE,GPIO_Pin_5))
		{
			key_flag2 = ~key_flag2;
		}
		if(get_key_value(GPIOE,GPIO_Pin_6))
		{
			key_flag3 = ~key_flag3;
		}
		if(get_key_value(GPIOC,GPIO_Pin_13))
		{
			key_flag4 = ~key_flag4;
		}
		drv_systick_delayms(10);		
	}
}
#endif
